package cn.jantd.modules.system.service.impl;

import cn.jantd.core.util.oConvertUtils;
import cn.jantd.modules.system.entity.*;
import cn.jantd.modules.system.mapper.SysRoleTeamuserMapper;
import cn.jantd.modules.system.mapper.SysSpeciatyMapper;
import cn.jantd.modules.system.mapper.SysTeamMapper;
import cn.jantd.modules.system.mapper.SysTeamUserMapper;
import cn.jantd.modules.system.service.ISysSpecialtyService;
import cn.jantd.modules.system.service.ISysTeamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @Author xiagf
 * @date 2019-07-04
 */
@Service
public class SysTeamServiceImpl extends ServiceImpl<SysTeamMapper, SysTeam> implements ISysTeamService{

    @Autowired
    private SysTeamMapper teamMapper;
    @Autowired
    private SysRoleTeamuserMapper sysRoleTeamuserMapper;
    @Autowired
    private SysTeamUserMapper sysTeamUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTeamWithRole(SysTeam team, String teamrole,String username) {
        this.save(team);
        if (oConvertUtils.isNotEmpty(teamrole)) {
            String[] arr1 = teamrole.split(",");
            for (String teamroleId : arr1) {
                SysRoleTeamuser teamRole = new SysRoleTeamuser(team.getId(), teamroleId);
                sysRoleTeamuserMapper.insert(teamRole);

            }
        }
        if (oConvertUtils.isNotEmpty(username)) {
            String[] arr2 = username.split(",");
            for (String userId : arr2) {
                SysTeamUser teamUser = new SysTeamUser(team.getId(), userId);
                sysTeamUserMapper.insert(teamUser);

            }
        }
    }

    @Override
    @CacheEvict(value = "loginUser_cacheRules", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void editTeamWithRole(SysTeam team, String teamrole,String username) {
        this.updateById(team);
        //先删后加
        sysRoleTeamuserMapper.delete(new QueryWrapper<SysRoleTeamuser>().lambda().eq(SysRoleTeamuser::getTeamId, team.getId()));
        if (oConvertUtils.isNotEmpty(teamrole)) {
            String[] arr3 = teamrole.split(",");
            for (String teamroleId : arr3) {
                SysRoleTeamuser teamRole = new SysRoleTeamuser(team.getId(), teamroleId);
                sysRoleTeamuserMapper.insert(teamRole);
            }
        }
         //用户名也是如此
        sysTeamUserMapper.delete(new QueryWrapper<SysTeamUser>().lambda().eq(SysTeamUser::getTeamId, team.getId()));
        if (oConvertUtils.isNotEmpty(username)) {
            String[] arr4 = username.split(",");
            for (String userId : arr4) {
                SysTeamUser teamUser = new SysTeamUser(team.getId(), userId);
                sysTeamUserMapper.insert(teamUser);
            }
        }

    }

}
